<template>
  <el-card>
    <el-row class="flex-center">
      <Logo></Logo>
    </el-row>
    <el-row class="flex-center">
      <h2>编辑个人资料</h2>
    </el-row>
    <el-form ref="userInfoForm" :model="formData" :rules="rules" size="medium" label-width="100px">
      <el-form-item label="用户ID：" prop="userId" label-width="520">
        <el-input v-model="formData.userId" placeholder="ID" clearable prefix-icon="el-icon-user"
                  disabled
                  class="form-item-input"></el-input>
      </el-form-item>
      <el-form-item label="账号：" prop="account" label-width="120">
        <el-input v-model="formData.account" placeholder="账号" clearable prefix-icon="el-icon-user"
                  disabled
                  class="form-item-input"></el-input>
      </el-form-item>
      <el-form-item label="邮箱：" prop="email" label-width="120">
        <el-input v-model="formData.email" placeholder="邮箱" clearable prefix-icon="el-icon-message"
                  class="form-item-input"></el-input>
      </el-form-item>
      <el-form-item label="用户名：" prop="userName" label-width="120">
        <el-input v-model="formData.userName" placeholder="用户名" clearable prefix-icon="el-icon-user"
                  class="form-item-input"></el-input>
      </el-form-item>
    </el-form>
    <el-button type="primary" class="form-item-input" @click="handleUpdateUserInfo">提交修改</el-button>
    <div class="flex-center bottom-help">
      <el-link type="primary" @click="go('/')">返回首页</el-link>
      <el-divider direction="vertical"></el-divider>
      <el-link type="primary" @click="go('/dashboard')">返回工作台</el-link>
    </div>
  </el-card>
</template>

<script>
import Logo from '@/components/Logo'
import {getInfo, getUserInfoByToken, updateUserInfo} from '@/api/login'
export default {
  name: 'UserInfoForm',
  components: {
    Logo
  },
  data () {
    return {
      formData: {
        userId: undefined,
        email: undefined,
        userName: undefined,
        account: undefined
      },
      formDataBakPack: {
        userId: undefined,
        email: undefined,
        userName: undefined,
        account: undefined
      },
      rules: {
        email: [
          this.$validate.email()
        ],
        userName: [
          this.$validate.length(1, 64, '用户名')
        ]
      }
    }
  },
  methods: {
    init () {
      getUserInfoByToken().then(res => {
        let data = {
          userId: res.data.userId,
          email: res.data.email,
          userName: res.data.userName,
          account: res.data.account
        }
        this.formData = data
        this.formDataBakPack = JSON.parse(data.toString())
      })
    },
    go (url) {
      this.$router.push(url)
    },
    handleUpdateUserInfo () {
      this.$refs['userInfoForm'].validate(valid => {
        if (valid) {
          updateUserInfo(this.formData).then(res => {
            this.$store.commit('SET_USERNAME', res.data.userName)
          }).catch(error => {
            this.$notify.error({
              title: '更新失败',
              message: error.message
            })
          })
        }
      })
      if (this.formDataBakPack.email === this.formData.email && this.formDataBakPack.userName === this.formData.userName) {
        this.$notify.warning('未做任何修改')
      } else {
        this.$notify.success('修改资料成功')
        // 当前登录用户是当前所查看项目创建者，则更新用户数据
        if (this.$store.getters.currentUserData.userId && this.$store.getters.currentUserData.userId === this.formData.userId) {
          getInfo(this.formData.userId).then(res => {
            this.$store.commit('SET_CURRENT_USER_DATA', res.data)
          })
        }
      }
      setTimeout(() => {
        this.go('/dashboard')
      }, 500)
    }
  },
  mounted () {
    this.init()
  }
}
</script>

<style lang="scss" scoped>
.flex-center {
  display: flex;
  justify-content: center;
}
.form-item-input {
  width: 100%;
}
.bottom-help {
  margin-top: 20px;
}
</style>
